package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcOtherOutService;
import com.vtuzx.kingdee.xqc_api.service.IXqcOtherService;
import com.vtuzx.kingdee.xqc_api.service.IXqcProduceService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 其他出库
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/other_out")
@RestController
public class XqcOtherOutController {

    /** 其他服务 */
    @Autowired
    private IXqcOtherOutService otherOutService;

    private Map<ObjectId,Long> lastRequestTimeMap = new ConcurrentHashMap<>();

    /**
     * 保存其他出库单(到创建阶段)
     */
    @PostMapping("/save_user_other_out")
    @VtuzxApi
    public Object saveUserOtherOut(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return otherOutService.saveUserOtherOut( _userId, name, param);
    }

    /**
     * 审核其他出库单
     */
    @PostMapping("/audit_other_out_order")
    @VtuzxApi
    public Object auditOtherOutOrder(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        //从map中查找上次请求的时间戳
        Long lastRequestTime = lastRequestTimeMap.get(_userId);
        //如果为空或者上次请求的时间戳与当前时间做差大于5s，则视为新请求，否则重复请求。
        if (lastRequestTime == null || System.currentTimeMillis() - lastRequestTime > 50000) {
            lastRequestTimeMap.put(_userId, System.currentTimeMillis());
            // 处理请求
            String stockNo = VtuzxUtil.getString(param, "stockNo");
            String billNo = VtuzxUtil.getString(param, "billNo");
            otherOutService.auditOtherOutOrder(_userId, name, stockNo, billNo);
        } else {
            // 重复请求
            throw new VtuzxException("正在出库，请不要重复点击");
        }

        return null;
    }

    /**
     * 添加用户临时其他出库物料箱号
     */
    @PostMapping("/add_user_otherout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
    } )
    public Object addUserOtherOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        int count = VtuzxUtil.getInt(param, "count");
        return otherOutService.addUserOtherOutMaterialBox(_userId, name, stockNo, boxNo, count);
    }

    /**
     * 添加用户临时其他出库物料箱号
     */
    @PostMapping("/add_user_otherout_material_box_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "boxList", label = "箱号列表", notNull = true),
    } )
    public Object addUserOtherOutMaterialBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        return otherOutService.addUserOtherOutMaterialBoxList(_userId, name, stockNo, boxList, billNo);
    }

    /**
     * 删除用户临时其他出库物料箱号
     */
    @PostMapping("/delete_user_otherout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserOtherOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        return otherOutService.deleteUserOtherOutMaterialBox(_userId, id);
    }

    /**
     * 删除用户临时其他出库物料
     */
    @PostMapping("/delete_user_otherout_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserOtherOutMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        otherOutService.deleteUserOtherOutMaterial(_userId, id);
        return null;
    }

    /**
     * 删除用户临时其他出库单(清空)
     */
    @PostMapping("/delete_user_otherout")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object deleteUserOtherOut(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        otherOutService.deleteUserOtherOut(_userId, stockNo);
        return null;
    }

    /**
     * 编辑用户临时其他出库物料箱号
     */
    @PostMapping("/update_user_otherout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object updateUserOtherOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return otherOutService.updateUserOtherOutMaterialBox(_userId, stockNo, materialNo, boxNo, count);
    }

    /**
     * 获取用户临时其他出库物料列表
     */
    @PostMapping("/get_user_otherout_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object getUserOtherOutMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return otherOutService.getUserOtherOutMaterial(_userId, stockNo, billNo);
    }

    /**
     * 获取用户临时其他出库物料箱号列表
     */
    @PostMapping("/get_user_otherout_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "_detailId", label = "detailId", notNull = true),
    } )
    public Object getUserOtherOutMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return otherOutService.getUserOtherOutMaterialBox(_userId, _detailId);
    }

    /**
     * 获取其他出库单
     */
    @PostMapping("/get_otherout_bill")
    @VtuzxApi
    public Object getOtherOutBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return otherOutService.getOtherOutBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取其他出库单分录列表
     */
    @PostMapping("/get_otherout_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getOtherOutBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return otherOutService.getOtherOutBillDetail(_userId, stockNo, billNo);
    }

    /**
     * 获取其他出库单分录的箱号详细
     */
    @PostMapping("/get_otherout_bill_detail_box")
    @VtuzxApi
    public Object getOtherOutBillDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return otherOutService.getOtherOutBillDetailBox(_userId, _detailId);
    }


    /**
     * 获取其它出库申请单
     */
    @PostMapping("/get_other_out_bill_apply")
    @VtuzxApi
    public Object getOtherOutBillApply(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        return otherOutService.getOtherOutBillApply(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取其他出库申请单详细
     */
    @PostMapping("/get_other_out_bill_apply_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "编号", notNull = true)
    } )
    public Object getOtherOutBillApplyDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return otherOutService.getOtherOutBillApplyDetail(_userId,  billNo);
    }

    /**
     * 保存其他出库申请单
     */
    @PostMapping("/save_other_out_Apply")
    @VtuzxApi
    public Object saveOtherOutApply(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return otherOutService.saveOtherOutApply( _userId, name, param);
    }

    /**
     * 获取其他出库单(PDA)
     */
    @PostMapping("/get_otherout_bill_PDA")
    @VtuzxApi
    public Object getOtherOutBillPDA(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        return otherOutService.getOtherOutBillPDA(_userId, param);
    }

    /**
     * 修改其他出库单打印状态
     * @param token
     * @param param
     * @return
     * @throws VtuzxException
     */
    @PostMapping("/update_other_out_seal")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object updateOtherOutSeal(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return otherOutService.updateOtherOutSeal(_userId, name, param);
    }

    /**
     * 下载其他出库详情
     */
    @PostMapping("/download_otherOut_detail")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadOtherOutDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return otherOutService.downloadOtherOutDetail(token, param);
    }
}
